package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorsMapper flavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void saveDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insert(dish);

        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (!flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(id));
            flavorsMapper.insert(flavors);
        }

    }

    @Override
    public PageResult findPage(DishPageQueryDTO dishDto) {

        PageHelper.startPage(dishDto.getPage(),dishDto.getPageSize());

        Page<DishVO> dishVOS = dishMapper.selectPage(dishDto);

        return new PageResult(dishVOS.getTotal(),dishVOS.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //起售中的菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //菜品被套餐相关联的不能删除
        for (Long id : ids) {
            List<Long> setMealIds = setmealDishMapper.getByDishId(id);
            if (!setMealIds.isEmpty()) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        //删除菜品的同时，将菜品对应的口味一并删除
        for (Long id : ids) {
            dishMapper.deleteById(id);

            flavorsMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO findById(Long id) {
        DishVO dishVO = new DishVO();

        Dish dish = dishMapper.getById(id);

        BeanUtils.copyProperties(dish,dishVO);

        //根据菜品id查询口味
        List<DishFlavor> flavors = flavorsMapper.getByDishId(id);

        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    @Transactional
    public void updateByIdWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateById(dish);

        //对关联口味表做修改（删除原口味，插入新口味）
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavorsMapper.deleteByDishId(dish.getId());

        if (!flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            flavorsMapper.insertFlavors(flavors);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
